#ifndef SERVEURTCP_H
#define SERVEURTCP_H

#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QList>
#include "model.h"

class IA_LIBSHARED_EXPORT ServeurTcp: public  QTcpServer
{
    Q_OBJECT
    public :
            ServeurTcp(QObject *parent=0);

			void lunch(const QString& port);
            QList<IAlib::AiCommand*> getCmdIn() const { return m_cmd_in; }

            void setCmdOut(const QList<IAlib::AiCommand*>& list_cmd);

            void clearCmdIn();

            void sendCmd(QTcpSocket * socket, QList<IAlib::AiCommand*> list_cmd);

            void setModel(IAlib::Model * model) { m_model = model; }

            void addCmd(IAlib::AiCommand * cmd);

			quint16 sendUpdate(IAlib::Model * model);

			void  sendAiModel();
            void  sendIts();

			void sendMonitoringEvent(const QString& time, const QString& id_agent, IAlib::Model::MonitoringEventType type, const QString& param);

            void sendItsAction(const IAlib::ItsAction& action);

    private slots :

            void connetionRequest();
            void readData();

            void reset_socket();

    signals :

            void cmdIn(QString sender, IAlib::AiCommand * cmd);
            void itsActionReceived(const IAlib::ItsAction& action);

            void socket_connected();
            void socket_disconnected();

    private :
           /*
              exchange with SIMFOR
            */
            void readSocket();

            IAlib::AiCommand * AiCommandFromByteArray(QDataStream &data);

            QTcpSocket * m_socket;
            QList<IAlib::AiCommand *> m_cmd_in;
            QList<IAlib::AiCommand *> m_cmd_out;

            quint16 m_block_size;

			QString       m_port;
            IAlib::Model * m_model;
};

#endif // SERVEURTCP_H
